package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Selection;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.InviteListBean;
import com.cljz.makepassenger.present.activity.InviteListP;
import com.cljz.makepassenger.ui.adapter.SendOrReceiveInviteAdapter;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.widget.ClearEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/12.
 */

public class InviteListActivity extends BaseActivity<InviteListP> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.cet_search)
    ClearEditText cetSearch;
    @BindView(R.id.xrv_invite_list)
    XRecyclerView xrvInviteList;
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    private List<InviteListBean.DataBean> inviteList = new ArrayList<>();
    private SendOrReceiveInviteAdapter sendOrReceiveInviteAdapter;
    private boolean isRefresh;
    private int page;
    private boolean isLoadMore;
    private int type;
    private String key;
    private View footView;


    @Override
    public void initData(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 0);
        tvTitleName.setText(type == 1 ? "发出的邀请" : "收到的邀请");
        footView = View.inflate(this, R.layout.item_foot_view, null);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvInviteList.setLayoutManager(linearLayoutManager);
        sendOrReceiveInviteAdapter = new SendOrReceiveInviteAdapter(this, inviteList);
        xrvInviteList.setAdapter(sendOrReceiveInviteAdapter);
        xrvInviteList.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvInviteList.setPullRefreshEnabled(true);
        xrvInviteList.setLoadingMoreEnabled(true);
        xrvInviteList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                page = 1;
                getP().getInviteList(page, type, key);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                getP().getInviteList(page, type, key);
            }
        });
        xrvInviteList.refresh();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_invite_list;
    }

    @Override
    public InviteListP newP() {
        return new InviteListP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                finish();
            }
        });
        RxTextView.textChanges(cetSearch)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
            @Override
            public void call(CharSequence charSequence) {
                Selection.setSelection(cetSearch.getText(), charSequence.length());
                if (charSequence.length() == 0) {
                    key = null;
                } else {
                    getP().getInviteList(page, type, charSequence.toString().trim());
                }
            }
        });
        sendOrReceiveInviteAdapter.setOnItemClickListener(new SendOrReceiveInviteAdapter.OnItemClickListener() {
            @Override
            public void onClick(InviteListBean.DataBean dataBean) {
                Router.newIntent((Activity) App.getInstance()).to(InvitationLetterActivity.class).putInt("type", 2).putString("id", dataBean.getInvite_id()).launch();
            }

            @Override
            public void onLongClick(InviteListBean.DataBean dataBean) {

            }
        });

    }

    public void getInviteListFail(String error) {
        L.e(error);
    }

    public void getInviteListSucceed(List<InviteListBean.DataBean> dataBeanList) {
        dismissLoadingDialog();
        inviteList = dataBeanList;
        if (isLoadMore) {
            xrvInviteList.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                xrvInviteList.setNoMore(true);
            } else {
                sendOrReceiveInviteAdapter.addData(dataBeanList);
                xrvInviteList.removeView(footView);
            }
            isLoadMore = false;
        } else if (isRefresh) {
            xrvInviteList.setNoMore(false);
            xrvInviteList.refreshComplete();
            sendOrReceiveInviteAdapter.setData(dataBeanList);
            isRefresh = false;
        } else {
            sendOrReceiveInviteAdapter.setData(dataBeanList);
        }
    }

    public XRecyclerView getXrvInviteList() {
        return xrvInviteList;
    }


}

